#ifndef UUID_C75F503308CC4F27864937BA27564546
#define UUID_C75F503308CC4F27864937BA27564546

#include "packet.hpp"
#include "packet_model.hpp"
#include <gvl/system/system.hpp>

namespace Psync5
{

struct PacketDataWriter
{
	PacketDataWriter(Packet& destPacketInit, uint32_t channelCountInit)
	: destPacket(destPacketInit)
	, packetModel(channelCountInit)
	{
		packetModel.reserveHeader(destPacket.reserve32());
	}

	bool addSection(SendSection& section)
	{
		int const maxHeaderSize = 5;

		if(destPacket.spaceLeft() < section.sizeBytes + maxHeaderSize)
			return false;

		packetModel.putSection(destPacket, section);

		section.lastSendMs = gvl::get_ticks();

		// Success, update model
		packetModel.finishSection(section);
		return true;
	}

	void finish()
	{
		packetModel.finish();
	}

	Packet& destPacket;
	PacketModel packetModel;
	uint32_t* header;
};

}

#endif // UUID_C75F503308CC4F27864937BA27564546
